home *** CD-ROM | disk | FTP | other *** search
/ Sound Fx / Sound Fx.iso / Software / UNZIPED / MPW181-5 / _SETUP.1 / sublay1.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-04-19  |  6.9 KB  |  240 lines

  1. /*  sublay1.cpp
  2.  
  3.     Implementation of layer I subband objects */
  4.  
  5. /*
  6.  *  @(#) subband_layer_1.cc 1.7, last edit: 6/15/94 16:51:49
  7.  *  @(#) Copyright (C) 1993, 1994 Tobias Bading (bading@cs.tu-berlin.de)
  8.  *  @(#) Berlin University of Technology
  9.  *
  10.  *  This program is free software; you can redistribute it and/or modify
  11.  *  it under the terms of the GNU General Public License as published by
  12.  *  the Free Software Foundation; either version 2 of the License, or
  13.  *  (at your option) any later version.
  14.  *
  15.  *  This program is distributed in the hope that it will be useful,
  16.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18.  *  GNU General Public License for more details.
  19.  *
  20.  *  You should have received a copy of the GNU General Public License
  21.  *  along with this program; if not, write to the Free Software
  22.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  23.  */
  24.  
  25. /*
  26.  *  Changes from version 1.1 to 1.2:
  27.  *    - scalefactors itself instead of scalefactor indices are stored in
  28.  *      SubbandLayer1... objects
  29.  *    - check for small values in [-1.0E-7, 1.0E-7] removed, because the
  30.  *      test itself was slower than some SynthesisFilter::input_sample() calls
  31.  *    - check for illegal scalefactor index 63 removed
  32.  */
  33.  
  34. #ifdef  __WIN32__
  35. #define STRICT
  36. #include <windows.h>
  37. #endif   // __WIN32__
  38.  
  39. #include "sublay1.h"
  40. #include "scalfact.h"
  41.  
  42. // factors and offsets for sample requantization:
  43. static const real table_factor[15] = {
  44.   0.0, (1.0/2.0) * (4.0/3.0), (1.0/4.0) * (8.0/7.0), (1.0/8.0) * (16.0/15.0),
  45.   (1.0/16.0) * (32.0/31.0), (1.0/32.0) * (64.0/63.0), (1.0/64.0) * (128.0/127.0),
  46.   (1.0/128.0) * (256.0/255.0), (1.0/256.0) * (512.0/511.0),
  47.   (1.0/512.0) * (1024.0/1023.0), (1.0/1024.0) * (2048.0/2047.0),
  48.   (1.0/2048.0) * (4096.0/4095.0), (1.0/4096.0) * (8192.0/8191.0),
  49.   (1.0/8192.0) * (16384.0/16383.0), (1.0/16384.0) * (32768.0/32767.0)
  50. };
  51.  
  52. static const real table_offset[15] = {
  53.   0.0, ((1.0/2.0)-1.0) * (4.0/3.0), ((1.0/4.0)-1.0) * (8.0/7.0), ((1.0/8.0)-1.0) * (16.0/15.0),
  54.   ((1.0/16.0)-1.0) * (32.0/31.0), ((1.0/32.0)-1.0) * (64.0/63.0), ((1.0/64.0)-1.0) * (128.0/127.0),
  55.   ((1.0/128.0)-1.0) * (256.0/255.0), ((1.0/256.0)-1.0) * (512.0/511.0),
  56.   ((1.0/512.0)-1.0) * (1024.0/1023.0), ((1.0/1024.0)-1.0) * (2048.0/2047.0),
  57.   ((1.0/2048.0)-1.0) * (4096.0/4095.0), ((1.0/4096.0)-1.0) * (8192.0/8191.0),
  58.   ((1.0/8192.0)-1.0) * (16384.0/16383.0), ((1.0/16384.0)-1.0) * (32768.0/32767.0)
  59. };
  60.  
  61. /**********************/    // used for single channel mode
  62. /*** Standard Class ***/    // and in derived class for intensity
  63. /**********************/    // stereo mode
  64.  
  65. SubbandLayer1::SubbandLayer1 (uint32 subbandnumber)
  66. {
  67.   this->subbandnumber = subbandnumber;
  68.   samplenumber = 0;
  69. }
  70.  
  71.  
  72. void SubbandLayer1::read_allocation (Ibitstream *stream, Header *, Crc16 *crc)
  73. {
  74.   if ((allocation = stream->get_bits (4)) == 15) ;
  75. //     cerr << "WARNING: stream contains an illegal allocation!\n";    // MPEG-stream is corrupted!
  76.  
  77.   if (crc)
  78.      crc->add_bits (allocation, 4);
  79.   if (allocation)
  80.   {
  81.      samplelength = allocation + 1;
  82.      factor = table_factor[allocation];
  83.     offset = table_offset[allocation];
  84.   }
  85. }
  86.  
  87.  
  88. void SubbandLayer1::read_scalefactor (Ibitstream *stream, Header *)
  89. {
  90.   if (allocation)
  91.      scalefactor = scalefactors[stream->get_bits (6)];
  92. }
  93.  
  94.  
  95. BOOL SubbandLayer1::read_sampledata (Ibitstream *stream)
  96. {
  97.   if (allocation)
  98.   {
  99.      sample = real (stream->get_bits (samplelength));
  100.   }
  101.   if (++samplenumber == 12)
  102.   {
  103.      samplenumber = 0;
  104.      return(TRUE);
  105.   }
  106.   return(FALSE);
  107. }
  108.  
  109.  
  110. BOOL SubbandLayer1::put_next_sample (e_channels channels,
  111.                       SynthesisFilter *filter1, SynthesisFilter *)
  112. {
  113.   if (allocation && channels != right)
  114.   {
  115.      register real scaled_sample = (sample * factor + offset) * scalefactor;
  116.      filter1->input_sample (scaled_sample, subbandnumber);
  117.   }
  118.   return(TRUE);
  119. }
  120.  
  121.  
  122. /******************************/
  123. /*** Intensity Stereo Class ***/
  124. /******************************/
  125.  
  126. SubbandLayer1IntensityStereo::SubbandLayer1IntensityStereo (uint32 subbandnumber)
  127. : SubbandLayer1 (subbandnumber)
  128. {
  129. }
  130.  
  131.  
  132. void SubbandLayer1IntensityStereo::read_scalefactor (Ibitstream *stream, Header *)
  133. {
  134.   if (allocation)
  135.   {
  136.      scalefactor = scalefactors[stream->get_bits (6)];
  137.      channel2_scalefactor = scalefactors[stream->get_bits (6)];
  138.   }
  139. }
  140.  
  141.  
  142. BOOL SubbandLayer1IntensityStereo::put_next_sample (e_channels channels,
  143.     SynthesisFilter *filter1, SynthesisFilter *filter2)
  144. {
  145.   if (allocation)
  146.   {
  147.     sample = sample * factor + offset;        // requantization
  148.      if (channels == both)
  149.     {
  150.         register real sample1 = sample * scalefactor,
  151.              sample2 = sample * channel2_scalefactor;
  152.         filter1->input_sample (sample1, subbandnumber);
  153.         filter2->input_sample (sample2, subbandnumber);
  154.      }
  155.      else if (channels == left)
  156.      {
  157.         register real sample1 = sample * scalefactor;
  158.         filter1->input_sample (sample1, subbandnumber);
  159.      }
  160.      else
  161.      {
  162.         register real sample2 = sample * channel2_scalefactor;
  163.         filter1->input_sample (sample2, subbandnumber);
  164.      }
  165.   }
  166.   return(TRUE);
  167. }
  168.  
  169.  
  170.  
  171. /********************/
  172. /*** Stereo Class ***/
  173. /********************/
  174.  
  175. SubbandLayer1Stereo::SubbandLayer1Stereo (uint32 subbandnumber)
  176. : SubbandLayer1 (subbandnumber)
  177. {
  178. }
  179.  
  180.  
  181. void SubbandLayer1Stereo::read_allocation (Ibitstream *stream, Header *, Crc16 *crc)
  182. {
  183.   allocation = stream->get_bits (4);
  184.   channel2_allocation = stream->get_bits (4);
  185.   if (crc)
  186.   {
  187.      crc->add_bits (allocation, 4);
  188.     crc->add_bits (channel2_allocation, 4);
  189.   }
  190.   if (allocation)
  191.   {
  192.      samplelength = allocation + 1;
  193.     factor = table_factor[allocation];
  194.     offset = table_offset[allocation];
  195.   }
  196.   if (channel2_allocation)
  197.   {
  198.     channel2_samplelength = channel2_allocation + 1;
  199.      channel2_factor = table_factor[channel2_allocation];
  200.      channel2_offset = table_offset[channel2_allocation];
  201.   }
  202. }
  203.  
  204.  
  205. void SubbandLayer1Stereo::read_scalefactor (Ibitstream *stream, Header *)
  206. {
  207.   if (allocation)
  208.      scalefactor = scalefactors[stream->get_bits (6)];
  209.   if (channel2_allocation)
  210.      channel2_scalefactor = scalefactors[stream->get_bits (6)];
  211. }
  212.  
  213.  
  214. BOOL SubbandLayer1Stereo::read_sampledata (Ibitstream *stream)
  215. {
  216.   BOOL returnvalue = SubbandLayer1::read_sampledata (stream);
  217.   if (channel2_allocation)
  218.   {
  219.      channel2_sample = real (stream->get_bits (channel2_samplelength));
  220.   }
  221.   return(returnvalue);
  222. }
  223.  
  224.  
  225. BOOL SubbandLayer1Stereo::put_next_sample (e_channels channels,
  226.                         SynthesisFilter *filter1, SynthesisFilter *filter2)
  227. {
  228.   SubbandLayer1::put_next_sample (channels, filter1, filter2);
  229.   if (channel2_allocation && channels != left)
  230.   {
  231.      register float sample2 = (channel2_sample * channel2_factor + channel2_offset) *
  232.                   channel2_scalefactor;
  233.      if (channels == both)
  234.         filter2->input_sample (sample2, subbandnumber);
  235.      else
  236.         filter1->input_sample (sample2, subbandnumber);
  237.   }
  238.   return(TRUE);
  239. }
  240.